<?php

function produceGraph($eid,$index)
{
	$_SESSION['indexGraph'.$index.'EID']=$eid;
	
	require_once ('design_files/graph_src/jpgraph.php');
	require_once ('design_files/graph_src/jpgraph_pie.php');
	require_once ('design_files/graph_src/jpgraph_pie3d.php');
	require_once ('design_files/graph_src/jpgraph_bar.php');
	require_once ('design_files/graph_src/jpgraph_line.php');
	ob_start();
	$con = mysql_connect('localhost','root','');
	mysql_select_db('sentana', $con);
	$dir = 'reports'; //bütün grafikler bu klasörün içinde olacak
	if(!file_exists($dir)) 
	{
		mkdir ($dir, 0777);
	}
	
	//search ettiğin keywordün entity idsiyle aynı olan bütün tweetlerin sentimentini al
	$sql="SELECT Title,MID FROM entities WHERE EID='".$eid."'";
	$result = mysql_query($sql,$con);
	
	while($row = mysql_fetch_array($result))
	{
		$_SESSION['indexGraph'.$index.'Title']=$row['Title'];
		$_SESSION['indexGraph'.$index.'Mid']=$row['MID'];
	}
	//

        $positive=0;
        $negative=0;

	$sql="SELECT count(*) As PosCount FROM tweet2entities WHERE EID='".$eid."' AND Sentiment='1'";
	$result = mysql_query($sql,$con);
	if( $row = mysql_fetch_array($result))
          $positive = $row["PosCount"];


        $sql="SELECT count(*) As NegCount FROM tweet2entities WHERE EID='".$eid."' AND Sentiment='-1'";
        $result = mysql_query($sql,$con);
        if( $row = mysql_fetch_array($result))
          $negative = $row["NegCount"];

	//graphı oluştur
	$data1=array(($positive/($positive+$negative))*100);
	$data2=array(($negative/($positive+$negative))*100);
	
	$graph = new Graph(200,100,'auto');
	$graph->SetScale("textlin");
	
	$theme_class=new UniversalTheme;
	$graph->SetTheme($theme_class);	
	$graph->SetBox(false);	
	$graph->ygrid->SetFill(false);	
	$graph->yaxis->HideLine(true);
	$graph->yaxis->HideTicks(true,true);
	$b1plot = new BarPlot($data1);
	$b2plot = new BarPlot($data2);
	$graph->xaxis->SetTickLabels(' ');
	$graph->xaxis->SetColor("lightblue","lightblue");
	$graph->SetBox(false);
	$graph->ygrid->Show(false);
	$graph->ygrid->SetFill(false);
	$graph->yaxis->HideLine(true);
	$graph->yaxis->HideTicks(true,true);
	$graph->yaxis->SetColor("lightblue","lightblue");	
	
	$gbplot = new GroupBarPlot(array($b1plot,$b2plot));
	$graph->Add($gbplot);
	$graph->title->Set($_SESSION['indexGraph'.$index.'Title']);
	$graph->title->SetColor("lightblue");
	
	$graph->SetBackgroundImage("design_files/black.png",BGIMG_FILLFRAME);
	@unlink("reports/".$_SESSION['userId']."i".$index.".png"); 
	$graph->Stroke("reports/".$_SESSION['userId']."i".$index.".png");
	ob_end_flush();
}
?>
<?php
function isRandomValExists($vals,$val)
{
	$flag=0;
	for($i=0;$i<sizeof($vals);$i++)
	{
		if($vals[$i]==$val)
		{
			$flag=1;
			break;
		}
	}
	
	return $flag;
}

?>
<?php
session_start();

$_SESSION['userId']=$_SERVER['REMOTE_ADDR'];
$con = mysql_connect('localhost','root','');
mysql_select_db('sentana', $con);

$i = 0;
//önce random 5 tane EID alalım
$sql = 	"SELECT EID, COUNT(*) AS OccCount FROM tweet2entities GROUP BY EID ORDER BY OccCount DESC LIMIT 3";
$result = mysql_query($sql, $con) or die(mysql_error());

while($row = mysql_fetch_array($result))   //aslında döngüye gerek yok sadece 1 tane dönecek ama öbür metodla saçmaladı
{
   $eid = $row['EID'];
   produceGraph($eid,$i);
   $i++;
}

header("location:indexMain.php");

?>
